package com.manager.system.service.impl;

import com.manager.common.dao.SuperDao;
import com.manager.common.utils.DateTools;
import com.manager.common.utils.ObjectUtils;
import com.manager.common.vo.PageVO;
import com.manager.model.Operation_Log;
import com.manager.model.System_User;
import com.manager.system.service.OperateLogService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 
* <p>Title: OperateLogServiceImp</p>
* <p>Description: 操作日志服务层，负责业务处理和调用数据的持久层</p>
* @author z.q.k
* @date 上午9:49:29
 */
@Transactional
@SuppressWarnings("all")
@Component
public class OperateLogServiceImpl extends SuperDao<Object> implements OperateLogService {

	@Override
	public void insertOperateLog(Operation_Log model) throws Exception
	{
		// TODO Auto-generated method stub
		if(model.getIp().equals("0:0:0:0:0:0:0:1")) {
			model.setIp("127.0.0.1");
		}
		super.addT(model);
	}

	@Override
	public PageVO getOperateLists(Operation_Log model) throws Exception {
		// TODO Auto-generated method stub
		List<Object> params = new ArrayList<Object>();
		StringBuffer sb = new StringBuffer();
		
		long startTime = 0;
		long endTime = 0;
		if(StringUtils.isNotBlank(model.getStartTime())) 
			startTime = DateTools.convert2long(model.getStartTime(),DateTools.NORMAL_M_FORMAT);
		
		if(StringUtils.isNotBlank(model.getEndTime())) 
			endTime = DateTools.convert2long(model.getEndTime(), DateTools.NORMAL_M_FORMAT);
		
		sb.append("FROM Operation_Log t  WHERE 1=1 ");
		
		if(StringUtils.isNotEmpty(model.getUserName())){
			sb.append(" AND t.userName LIKE # ");
			params.add("%" + model.getUserName() + "%");
		}
		
		if(StringUtils.isNotBlank(model.getRoleName())) {
			sb.append(" AND t.roleName LIKE # ");
			params.add("%" + model.getRoleName() + "%");
		}
		
		if(StringUtils.isNotBlank(model.getStartTime())) {
			sb.append(" AND t.createTime >= # ");
			params.add(startTime);
		}
		
		if(StringUtils.isNotBlank(model.getEndTime())) {
			sb.append(" AND t.createTime <= # ");
			params.add(endTime);
		}
		
		String sql = ObjectUtils.replaceIndexByList(sb.toString(), params);
		
		List<Object> list = this.getList(sql, params.toArray(), model.getStart(), model.getLimit(), " order by t."+model.getOrderby()+"  " + model.getDesc());
		int sum = this.getListCount("SELECT count(*) " + sql, params.toArray(), "");
		int count = (int)Math.ceil(Double.valueOf(sum)/Double.valueOf(model.getLimit()));

		return PageVO.create(model.getStart(),count,list);
	}

	
	public void getOperationInfo(List<Operation_Log> nList, Operation_Log oLog, System_User user) throws Exception {
		
		nList.add(oLog);
		for(Operation_Log model : nList) {
			if(model.getId().equals(oLog.getId())) {
				if(user == null) oLog.setTemp1("admin");
				else oLog.setTemp1(user.getUserName());
			}
		}
	}
	
	
	@Override
	@Transactional
	public int deleteOperateLogs(List<Operation_Log> logs) {
		// TODO Auto-generated method stub
		try {
			
			for(Operation_Log log : logs) {
				Operation_Log entity = (Operation_Log)this.getT(new Operation_Log(), log.getId());
				this.delete(entity);
			}
			return 1;
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
		}
		
		
	}

	@Override
	public List<Object> getExportData(Operation_Log model) {
		List<Object> params = new ArrayList<Object>();
		StringBuffer sb = new StringBuffer();
		
		long startTime = 0;
		long endTime = 0;
		if(StringUtils.isNotBlank(model.getStartTime())) 
			startTime = DateTools.convert2long(model.getStartTime(),DateTools.NORMAL_M_FORMAT);
		
		if(StringUtils.isNotBlank(model.getEndTime())) 
			endTime = DateTools.convert2long(model.getEndTime(), DateTools.NORMAL_M_FORMAT);
		
		sb.append("FROM Operation_Log t  WHERE 1=1 ");
		if(StringUtils.isNotEmpty(model.getUserName())){
			sb.append(" AND t.userName LIKE ? ");
			params.add("%" + model.getUserName() + "%");
		}
		
		if(StringUtils.isNotBlank(model.getRoleName())) {
			sb.append(" AND t.roleName LIKE ? ");
			params.add("%" + model.getRoleName() + "%");
		}
		
		if(StringUtils.isNotBlank(model.getStartTime())) {
			sb.append(" AND t.createTime >= ? ");
			params.add(startTime);
		}
		
		if(StringUtils.isNotBlank(model.getEndTime())) {
			sb.append(" AND t.createTime <= ? ");
			params.add(endTime);
		}
		List<Object> list = this.getList(sb.toString(), params.toArray(), 0, 2000, "");
		return list;
	}
	
}
